package com.ruoyi.classai.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.ruoyi.classai.config.websocket.WebSocket;
import com.ruoyi.common.security.annotation.InnerAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.classai.domain.EwcStudent;
import com.ruoyi.classai.service.IEwcStudentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学生信息Controller
 * 
 * @author lcj
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/student")
public class EwcStudentController extends BaseController
{
    @Autowired
    private IEwcStudentService ewcStudentService;
    @Autowired
    private WebSocket webSocket;

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("classai:student:list")
    @GetMapping("/list")
    public TableDataInfo list(EwcStudent ewcStudent)
    {
        startPage();
        List<EwcStudent> list = ewcStudentService.list(Wrappers.query(ewcStudent));
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("classai:student:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EwcStudent ewcStudent)
    {
        List<EwcStudent> list = ewcStudentService.list(Wrappers.query(ewcStudent));
        ExcelUtil<EwcStudent> util = new ExcelUtil<EwcStudent>(EwcStudent.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @RequiresPermissions("classai:student:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ewcStudentService.getById(id));
    }

    /**
     * 新增学生信息
     */
    @RequiresPermissions("classai:student:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EwcStudent ewcStudent)
    {
        return toAjax(ewcStudentService.save(ewcStudent));
    }

    /**
     * 修改学生信息
     */
    @RequiresPermissions("classai:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody EwcStudent ewcStudent)
    {
        return toAjax(ewcStudentService.updateById(ewcStudent));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("classai:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ewcStudentService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 学生举手
     */
    @InnerAuth
    @GetMapping("/getStudentHand")
    public AjaxResult getStudentHand(@NotBlank(message = "学生id不能为空")String studenId,
                                     @NotBlank(message = "老师id不能为空")String teacherId) {
        //查询学生信息
        EwcStudent student=ewcStudentService.getById(studenId);
        if (Objects.isNull(student)){
            return AjaxResult.error("没有查询到学生信息");
        }
        //推送信息
        JSONObject json=new JSONObject();
        json.put("type",1);
        json.put("data",student);
        webSocket.pushMessage(teacherId, json.toJSONString());

        return AjaxResult.success("推送成功");
    }
}
